package com.ihighest.app.infox.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.AddressAdapter;
import com.ihighest.app.infox.bean.AddressListItem;
import com.ihighest.app.infox.db.DBManager;

public class Selectcity extends Activity implements OnClickListener {
	private SQLiteDatabase db;
	private DBManager dbm;
	private ListView provinceListView = null;
	private ListView cityListView = null;
	private ListView areaListView3 = null;
	private String province = null;
	private String city = null;
	private String district = null;
	AddressAdapter myAdapter1, myAdapter2, myAdapter3;
	AddressListItem myListItem1, myListItem2, myListItem3;
	private Intent myintent;
	private LinearLayout cityparent;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.infox_activity_query_address);
		provinceListView = (ListView) findViewById(R.id.listView1);
		cityListView = (ListView) findViewById(R.id.listView2);
		areaListView3 = (ListView) findViewById(R.id.listView3);
		myintent = getIntent();
		cityparent = (LinearLayout) findViewById(R.id.id_cityparentlayout);

		WindowManager wm = this.getWindowManager();

		int width = wm.getDefaultDisplay().getWidth();
		int height = wm.getDefaultDisplay().getHeight();
		android.view.ViewGroup.LayoutParams lp = cityparent.getLayoutParams();
		lp.height = height / 2;
		cityparent.setLayoutParams(lp);
		// savecity();

		initListView1();
	}

	public void initListView1() {
		dbm = new DBManager(this);
		dbm.openDatabase();
		db = dbm.getDatabase();
		List<AddressListItem> list = new ArrayList<AddressListItem>();

		try {
			String sql = "select * from city where parentid  is null";
			Cursor cursor = db.rawQuery(sql, null);
			cursor.moveToFirst();
			while (!cursor.isLast()) {
				String code = cursor.getString(cursor.getColumnIndex("uuid"));
				String name = cursor.getString(cursor.getColumnIndex("areaname"));
				myListItem1 = new AddressListItem();
				myListItem1.setName(name);
				myListItem1.setPcode(code);
				list.add(myListItem1);
				cursor.moveToNext();
			}
			String code = cursor.getString(cursor.getColumnIndex("uuid"));
			String name = cursor.getString(cursor.getColumnIndex("areaname"));
			AddressListItem myListItem = new AddressListItem();
			myListItem.setName(name);
			myListItem.setPcode(code);
			list.add(myListItem);
		} catch (Exception e) {
		}
		dbm.closeDatabase();
		db.close();

		String code = list.get(0).getPcode();
		province = list.get(0).getName();
		initListView2(code);
		myAdapter1 = new AddressAdapter(this, list);
		provinceListView.setAdapter(myAdapter1);
		provinceListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				province = ((AddressListItem) arg0.getItemAtPosition(arg2)).getName();
				String pcode = ((AddressListItem) arg0.getItemAtPosition(arg2)).getPcode();
				initListView2(pcode);
				myAdapter1.changeSelected(arg2);
			}
		});
	}

	public void initListView2(String pcode) {
		dbm = new DBManager(this);
		dbm.openDatabase();
		db = dbm.getDatabase();
		List<AddressListItem> list = new ArrayList<AddressListItem>();

		try {
			String sql = "select * from city where parentid='" + pcode + "'" + "order by uuid asc";
			Cursor cursor = db.rawQuery(sql, null);
			cursor.moveToFirst();
			while (!cursor.isLast()) {
				String code = cursor.getString(cursor.getColumnIndex("uuid"));
				String name = cursor.getString(cursor.getColumnIndex("areaname"));
				AddressListItem myListItem = new AddressListItem();
				myListItem.setName(name);
				myListItem.setPcode(code);
				list.add(myListItem);
				cursor.moveToNext();
			}
			String code = cursor.getString(cursor.getColumnIndex("uuid"));
			String name = cursor.getString(cursor.getColumnIndex("areaname"));
			myListItem2 = new AddressListItem();
			myListItem2.setName(name);
			myListItem2.setPcode(code);
			list.add(myListItem2);
		} catch (Exception e) {
		}
		dbm.closeDatabase();
		db.close();

		// 榛樿閫変腑绗�涓煄甯傦紝骞跺垵濮嬪寲璇ュ煄甯備笅鐨勫尯鍩熷垪琛�
		String code = list.get(0).getPcode();
		city = list.get(0).getName();
		initListView3(code);

		myAdapter2 = new AddressAdapter(this, list);
		cityListView.setAdapter(myAdapter2);
		cityListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				city = ((AddressListItem) arg0.getItemAtPosition(arg2)).getName();
				String pcode = ((AddressListItem) arg0.getItemAtPosition(arg2)).getPcode();
				initListView3(pcode);
				myAdapter2.changeSelected(arg2);
			}
		});
	}

	public void initListView3(String pcode) {
		dbm = new DBManager(this);
		dbm.openDatabase();
		db = dbm.getDatabase();
		List<AddressListItem> list = new ArrayList<AddressListItem>();
		try {
			String sql = "select * from city where parentid='" + pcode + "'";
			Cursor cursor = db.rawQuery(sql, null);
			cursor.moveToFirst();
			while (!cursor.isLast()) {
				String code = cursor.getString(cursor.getColumnIndex("uuid"));
				String name = cursor.getString(cursor.getColumnIndex("areaname"));
				myListItem3 = new AddressListItem();
				myListItem3.setName(name);
				myListItem3.setPcode(code);
				list.add(myListItem3);
				cursor.moveToNext();
			}
			String code = cursor.getString(cursor.getColumnIndex("uuid"));
			String name = cursor.getString(cursor.getColumnIndex("areaname"));
			myListItem2 = new AddressListItem();
			AddressListItem myListItem = new AddressListItem();
			myListItem.setName(name);
			myListItem.setPcode(code);
			list.add(myListItem);

		} catch (Exception e) {
		}
		dbm.closeDatabase();
		db.close();
		myAdapter3 = new AddressAdapter(this, list);
		areaListView3.setAdapter(myAdapter3);
		areaListView3.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				// TODO Auto-generated method stub
				district = ((AddressListItem) arg0.getItemAtPosition(arg2)).getName();
				myAdapter3.changeSelected(arg2);
				// Toast.makeText(Selectcity.this, province + "-" + city + "-" +
				// district, 2000).show();
				myintent.putExtra("adress", province + city + district);
				myintent.putExtra("city", city);

				setResult(-1, myintent);
				finish();

			}
		});
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub

	}

	private void savecity() {
		// TODO Auto-generated method stub
		dbm = new DBManager(this);
		dbm.openDatabase();
		db = dbm.getDatabase();

		// 数据插入
		// Api.data("", new ApiCallback(this,true) {
		//
		// @Override
		// public void handleSuccess(ApiResult result) {
		// // TODO Auto-generated method stub
		//
		// }
		//
		// @Override
		// public void handleFailure(Exception e, String msg) {
		// // TODO Auto-generated method stub
		//
		// }
		// }, this);

		// String
		// sql="insert into city (uuid,areaname,parentid)values("+id+","+name+","+parentid+")";

		// db.execSQL(sql);

		dbm.closeDatabase();
		db.close();

	}

}